﻿/*+===================================================================
File:      DiverBullet.cpp

Summary:   Định nghĩa các phương thức của CDiverBullet.
===================================================================+*/

#include "EnemyDiverBullet.h"
#include "Gun.h"
#include "Explosion.h"
#include "PlayState.h"

#define HEIGHT_EXPLODE 250

CEnemyDiverBullet::CEnemyDiverBullet(D3DXVECTOR2 pos, int id, int typeID, float angle) :
CBullet(pos, id, typeID, angle)
{
	m_pSprite = m_pResourceManager->GetSprite(ID_SPRITE_S2_BULLET);
	m_posExplodeY = pos.y + HEIGHT_EXPLODE;

	m_v.x = 0.0f;
	m_v.y = 300.0f;

	m_angle = angle;

	m_IsExplode = false;

	m_lastGroundCollision = NULL;

	m_box.height = 12;
	m_box.width = 12;
}


CEnemyDiverBullet::~CEnemyDiverBullet()
{
	if (m_lastGroundCollision)
		delete m_lastGroundCollision;
}

void CEnemyDiverBullet::Update(float timeFrame)
{
	if (!m_bIsLive)
		return;

	// Ra khỏi màn hình thì mất đi.

	D3DXVECTOR2 posInViewPort = CViewport::GetInstance()->TransformMatrix(m_pos);
	if (posInViewPort.x < 0 || posInViewPort.x > SCREEN_WIDTH || posInViewPort.y < 0 || posInViewPort.y > SCREEN_HEIGHT)
	{
		m_bIsLive = false;
		m_pContainerGun->ReloadBullet();
		return;
	}

	/*if ( posInViewPort.y < 0)
	{
		m_bIsLive = false;
		m_pContainerGun->ReloadBullet();
		return;
	}*/

	// Đến 1 độ cao quy định thì nổ ra 3 viên.
	if (!m_IsExplode && m_pos.y >= m_posExplodeY)
	{
		m_IsExplode = true;
		m_a.y = -500.0f;
		m_v.x = 220.0f * sin(m_angle * PI / 180.0f);
		m_v.y *= cos(m_angle * PI / 180.0f);		
		this->Explode();
	}

	UpdateBox(timeFrame);
}

void CEnemyDiverBullet::UpdateCollision(vector<CGameObject*> vecGameObjs, float timeFrame)
{
	if (!m_bIsLive)
		return;

	float timeCollision = timeFrame;
	float normalx = 0.0f, normaly = 0.0f;
	CGameObject* objCollision = NULL; // Đối tượng va chạm sớm nhất.

	for (int i = 0; i < vecGameObjs.size(); i++)
	{
		if (vecGameObjs[i]->IsLive())
		{
			float t = CheckCollision(vecGameObjs[i], normalx, normaly, timeFrame);

			if (t < timeFrame && t < timeCollision)
			{
				if ((vecGameObjs[i]->GetTypeId() == ID_GRASS || 
					vecGameObjs[i]->GetTypeId() == ID_GRASS_CAN_FALL)
					&& normaly == 1)
				{
					objCollision = vecGameObjs[i];
					timeCollision = t;
				}
			}
		}
	}

	// Xử lí va chạm
	if (objCollision != NULL)
	{
		// Va chạm với đất thứ 2 mới nổ
		if (g_nCurentLvl == ID_LEVEL2)
		{
			if (m_lastGroundCollision == NULL)
			{
				m_lastGroundCollision = objCollision;
			}
			else
			{
				m_v += m_a * timeCollision;
				m_a.y = 0.0f;

				this->Kill();
			}
		}
		else
		{
			m_v += m_a * timeCollision;
			m_a.y = 0.0f;

			this->Kill();
		}
	}

	// Update vị trí.
	m_pos += m_v * timeFrame;
	m_v += m_a * timeFrame;
}

void CEnemyDiverBullet::Draw()
{
	if (!m_bIsLive)
		return;
	D3DXVECTOR2 pos;
	pos = CViewport::GetInstance()->TransformMatrix(m_pos);

	m_pSprite->Draw(D3DXVECTOR2((INT)pos.x, (INT)pos.y));
}

bool CEnemyDiverBullet::Kill()
{
	m_bIsLive = false;
	m_pContainerGun->ReloadBullet();

	CExplosion* explosion = new CExplosion(m_pos, 0, ID_ENEMY_EXPLOSION, true);
	CPlayState::s_objsOutSideQuadTree.push_back(explosion);
	return true;
}

void CEnemyDiverBullet::Explode()
{
	if (m_nId == 0)
	{
		CExplosion* ex = new CExplosion(m_pos, 0, ID_BIG_EXPLOSION);
		CPlayState::s_objsOutSideQuadTree.push_back(ex);
	}
}